package com.jxc.action.qxgl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 用户管理
 * 
 * @author Tino
 * 
 */
@Namespace("/qxgl")
@Action(value = "userAction", results = {
		@Result(name = "user", location = "/user/user.jsp"),
		@Result(name = "userAdd", location = "/user/userAdd.jsp"),
		@Result(name = "userEdit", location = "/user/userEdit.jsp") })
public class UserAction extends BaseAction implements ModelDriven<VUser> {
	private final static Logger logger = Logger.getLogger(UserAction.class);

	private VUser vUser = new VUser();

	@Autowired
	private UserServiceI userService;
	@Autowired
	private RepertoryServiceI repertoryService;

	/**
	 * 代理商下拉列表
	 */
	public void noAuth_agencyCombobox() {
		vUser.setUserType(2);
		this.writeJson(userService.combobox(vUser));
	}
	/**
	 * 经销商品牌的代理商，并且还未授权的
	 */
	public void noAuth_newAgencyCombobox(){
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if(vuser.getReid()==null){
			writeJson(userService.agency(vuser));
		}
	}
	
	/**
	 * 当前用户所属或所拥有仓库下的所有员工下拉框
	 */
	public void noAuth_userInReperCombobox(){
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (vuser.getReid() != null) {
			vUser.setReid(vuser.getReid());
			this.writeJson(userService.combobox(vUser));
		} else {
			List<VUser> vusers = new ArrayList<VUser>();
			vusers.add(vuser);
			if(vUser.getReid()!=null){
				vusers.addAll(userService.combobox(vUser));
			}else{
				VRepertory vrepertory = new VRepertory();
				vrepertory.setUid(sessionInfo.getUserId());
				List<VRepertory> vrepertories = repertoryService.list(vrepertory);
				if (vrepertories != null && vrepertories.size() > 0) {
					for (VRepertory vre : vrepertories) {
						vUser.setReid(vre.getId());
						vusers.addAll(userService.combobox(vUser));
					}
				}
			}
			this.writeJson(vusers);
		}
	}

	@Override
	public VUser getModel() {
		return vUser;
	}

	public String user() {
		getSubAuths("QXGL_YHGL");
		return "user";
	}

	public String userAdd() {
		return "userAdd";
	}

	public String userEdit() {
		return "userEdit";
	}

	public String userRoleEdit() {
		return "userRoleEdit";
	}

	public void add() {
		Json j = new Json();
		try {
			userService.save(vUser);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败，用户名已存在！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			userService.update(vUser);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败，用户名已存在！");
		}
		super.writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		userService.delete(vUser.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}

	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		if(sessionInfo.getType()==0){
			super.writeJson(userService.datagrid(vUser));
		}else{
			VUser vuser = userService.get(sessionInfo.getUserId());
			super.writeJson(userService.datagrid(vUser,vuser));
			
		}
	}

	public void combobox() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		if(sessionInfo.getType()==0){
			super.writeJson(userService.datagrid(vUser));
		}else{
			VUser vuser = null;
			if(sessionInfo.getType()==1){
				vuser = userService.get(sessionInfo.getUserId());
			}else{
				vuser = userService.get(repertoryService.get(Long.parseLong(sessionInfo.getRepertoryIds())).getUid());
			}
			super.writeJson(userService.datagrid(vUser,vuser));
			
		}
		super.writeJson(userService.combobox(vUser));
	}

}
